package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbBrandServiceI;
import com.dongdongshop.service.TbSpecificationServiceI;
import com.dongdongshop.service.TbTypeTemplateServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("tbTypeTemplateController")
public class TbTypeTemplateController {


    @Reference//模板
    private TbTypeTemplateServiceI tbTypeTemplateServiceI;

    private Logger logger= LoggerFactory.getLogger(TbBrandController.class);

    @Reference //商品管理
    private TbBrandServiceI tbBrandServiceI;

    @Reference //规格
    private TbSpecificationServiceI tbSpecificationServiceI;

    @RequestMapping("getFindAll")
    public String getFindAll(Model model){
        //商品管理
        List<TbBrand> tbBrands = tbBrandServiceI.getFindAll();
        model.addAttribute("tbBrands",tbBrands);
        //规格
        List<TbSpecification> tbSpecifications = tbSpecificationServiceI.getFindAll();
        model.addAttribute("tbSpecifications",tbSpecifications);
        //规格
        return "admin/type_template";
    }


    //查询模板
    @RequestMapping("getFindAlls")
    @ResponseBody
    public SpecificPrint getFindAlls(String name){
        try {
            List<TbTypeTemplate> tbTypeTemplates = tbTypeTemplateServiceI.getFindAll(name);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbTypeTemplates);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbTypeTemplates);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }

    }

    //增加模板
    @RequestMapping("saveTbTypeTemplate")
    @ResponseBody
    public SpecificPrint saveTbTypeTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            tbTypeTemplateServiceI.saveTbTypeTemplate(tbTypeTemplate);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbTypeTemplate);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbTypeTemplate);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //删除
    @RequestMapping("deleteTbTypeTemplate")
    @ResponseBody
    public SpecificPrint deleteTbTypeTemplate(String ids){
        try {
            //把选中的符号替换掉
            String id =ids.replaceAll("'","");
            tbTypeTemplateServiceI.deleteTbTypeTemplate(id);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",id);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(id);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //回显
    @RequestMapping("toUpdate")
    @ResponseBody
    public TbTypeTemplate toUpdate(Integer id){
        return tbTypeTemplateServiceI.selectTbTypeTemplateById(id);
    }

    //确认修改
    @RequestMapping("updateTbTypeTemplate")
    @ResponseBody
    public SpecificPrint updateTbTypeTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            tbTypeTemplateServiceI.updateTbTypeTemplate(tbTypeTemplate);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbTypeTemplate);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbTypeTemplate);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }
}
